/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package com.xy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.xy.entity.CertificateApplyEntity;
import com.xy.entity.CertificateTempEntity;
import com.xy.entity.CertificateTypeEntity;
import com.xy.service.ICertificateApplyService;
import com.xy.service.ICertificateTempService;
import com.xy.service.ICertificateTypeService;
import com.xy.vo.CertificateTempVO;
import com.xy.wrapper.CertificateTempWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.springblade.core.mp.support.Condition.getQueryWrapper;

/**
 * 用户-证类型中间表 控制器
 *
 * @author BladeX
 * @since 2023-07-25
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-certificateTemp/certificateTemp")
@Api(value = "用户-证类型中间表", tags = "用户-证类型中间表接口")
public class CertificateTempController extends BladeController {

	private final ICertificateTempService certificateTempService;
	private final ICertificateTypeService certificateTypeService;
	private final ICertificateApplyService certificateApplyService;

	/**
	 * 用户-证类型中间表 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入certificateTemp")
	public R<CertificateTempVO> detail(CertificateTempEntity certificateTemp) {
		CertificateTempEntity detail = certificateTempService.getOne(Condition.getQueryWrapper(certificateTemp));
		List<CertificateTypeEntity> list = certificateTypeService.list();
		Map<String, CertificateTypeEntity> typeMap = new HashMap<>();
		if (Func.isNotEmpty(list)) {
			typeMap = list.stream().collect(Collectors.toMap(t->Func.toStr(t.getId()), s -> s, (v1, v2) -> v1));
		}
		Map<String, List<CertificateApplyEntity>> countMap = certificateApplyService.countByUserIds(Collections.singletonList(detail.getCertificateTypeId()));
		return R.data(CertificateTempWrapper.build().entityVO1(detail, typeMap, countMap));
	}

	/**
	 * 用户-证类型中间表 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入certificateTemp")
	public R<IPage<CertificateTempVO>> list(@ApiIgnore @RequestParam Map<String, Object> certificateTemp, Query query) {
		IPage<CertificateTempEntity> pages = certificateTempService.page(Condition.getPage(query), Condition.getQueryWrapper(certificateTemp, CertificateTempEntity.class));
		Map<String, CertificateTypeEntity> typeMap = new HashMap<>();
		if (Func.isNotEmpty(pages.getRecords())) {
			List<CertificateTypeEntity> list = certificateTypeService.list();
			if (Func.isNotEmpty(list)) {
				typeMap = list.stream().collect(Collectors.toMap(t->Func.toStr(t.getId()), s -> s, (v1, v2) -> v1));
			}
			List<Long> certificateTypeIds = pages.getRecords().stream().map(CertificateTempEntity::getCertificateTypeId).distinct().collect(Collectors.toList());
			Map<String, List<CertificateApplyEntity>> longListMap = certificateApplyService.countByUserIds(certificateTypeIds);


			return R.data(CertificateTempWrapper.build().pageVO1(pages, typeMap, longListMap));
		}
		return R.data(CertificateTempWrapper.build().pageVO(pages));

	}
	@ApiOperation(value = "证件中间表回写返回",notes = "传入uerId")
	@GetMapping("/all")
	public R<List<CertificateTempVO>> getById(CertificateTempEntity certificateTemp) {
		if(Func.isEmpty(certificateTemp.getUserId())){
			return R.fail("请传入用户ID");
		}
		LambdaQueryWrapper<CertificateTempEntity> lqw = Wrappers.<CertificateTempEntity>query().lambda().eq(CertificateTempEntity::getUserId,certificateTemp.getUserId()).eq(CertificateTempEntity::getTenantId, "929881");
		List<CertificateTempEntity> list = certificateTempService.list(lqw);
		Map<String, CertificateTypeEntity> typeMap = new HashMap<>();
		if (Func.isNotEmpty(list)) {
			List<CertificateTypeEntity> listType = certificateTypeService.list();
			if (Func.isNotEmpty(list)) {
				typeMap = listType.stream().collect(Collectors.toMap(t->Func.toStr(t.getId()), s -> s, (v1, v2) -> v1));
			}
			List<Long> certificateTypeIds = list.stream().map(CertificateTempEntity::getCertificateTypeId).distinct().collect(Collectors.toList());
			Map<String, List<CertificateApplyEntity>> longListMap = certificateApplyService.countByUserIds(certificateTypeIds);
			return R.data(CertificateTempWrapper.build().listVO1(list, typeMap, longListMap));
		}
		return R.data(CertificateTempWrapper.build().listVO1(list,typeMap,new HashMap<>()));
	}

	/**
	 * 用户-证类型中间表 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入certificateTemp")
	public R save(@Valid @RequestBody CertificateTempEntity certificateTemp) {
		CertificateTypeEntity quer = new CertificateTypeEntity();
		quer.setId(certificateTemp.getCertificateTypeId());
		quer.setTenantId(AuthUtil.getTenantId());
		long count = certificateTypeService.count(getQueryWrapper(quer));
		if (count == 0) {
			return R.fail("证件类型不存在");
		}
		if (Func.isEmpty(certificateTemp.getId())) {
			CertificateTempEntity tempEntity = new CertificateTempEntity();
			quer.setId(certificateTemp.getCertificateTypeId());
			quer.setTenantId(AuthUtil.getTenantId());
			long count1 = certificateTempService.count(getQueryWrapper(tempEntity));
			if (count1 > 0) {
				return R.fail("该展商已经有这个证件类型了");
			}
		}
		return R.status(certificateTempService.save(certificateTemp));
	}

	/**
	 * 用户-证类型中间表 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入certificateTemp")
	public R update(@Valid @RequestBody CertificateTempEntity certificateTemp) {
		if (Func.isNull(certificateTemp.getCertificateTypeId())) {
			return R.fail("请选择要修改的的证件类型");
		}
		return R.status(certificateTempService.updateById(certificateTemp));
	}

	/**
	 * 用户-证类型中间表 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入certificateTemp")
	public R submit(@Valid @RequestBody CertificateTempEntity certificateTemp) {

		CertificateTypeEntity quer = new CertificateTypeEntity();
		quer.setId(certificateTemp.getCertificateTypeId());
		quer.setTenantId(AuthUtil.getTenantId());
		long count = certificateTypeService.count(getQueryWrapper(quer));
		if (count == 0) {
			return R.fail("证件类型不存在");
		}

		if (Func.isEmpty(certificateTemp.getId())) {
			CertificateTempEntity tempEntity = new CertificateTempEntity();
			quer.setId(certificateTemp.getCertificateTypeId());
			quer.setTenantId(AuthUtil.getTenantId());
			long count1 = certificateTempService.count(getQueryWrapper(tempEntity));
			if (count1 > 0) {
				return R.fail("该展商已经有这个证件类型了");
			}
		} else {
			//已经存在A，B数据，把B的类型修改成A时候验证
			LambdaQueryWrapper<CertificateTempEntity> lqw = Wrappers.<CertificateTempEntity>query().lambda().notIn(CertificateTempEntity::getId, certificateTemp.getId())
				.eq(CertificateTempEntity::getCertificateTypeId, certificateTemp.getCertificateTypeId()).eq(CertificateTempEntity::getTenantId, AuthUtil.getTenantId());
			long count1 = certificateTempService.count(lqw);
			if (count1 > 0) {
				return R.fail("该展商已经有这个证件类型了");
			}
		}
		return R.status(certificateTempService.saveOrUpdate(certificateTemp));
	}

	/**
	 * 用户-证类型中间表 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(certificateTempService.deleteLogic(Func.toLongList(ids)));
	}

}
